package com.tanhua.dubbo.server.api.mongo;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryTodayBest(Long userId) {
        // 条件
        Criteria criteria = Criteria.where("toUserId").is(userId);
        // 按照得分倒序排序，获取第一条数据
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        return this.mongoTemplate.findOne(query, RecommendUser.class);
    }


    @Override
    public PageResult queryRecommendUserList(Long userId, Integer page, Integer size) {
        Query query = new Query(Criteria.where("toUserId").is(userId))
                .with(PageRequest.of(page-1,size));
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page,size,0,list);
    }

}
